//
//  WGCBaseRequest.m
//  WisdomGatewayProject
//
//  Created by JianWei Chen on 2017/6/1.
//  Copyright © 2017年 JianWei Chen. All rights reserved.
//

#import "DTCBaseRequest.h"
#import <objc/runtime.h>

@interface DTCBaseRequest ()

@end

@implementation DTCBaseRequest

NSString * const WGCBaseRequest_instance = @"instance";

+ (void)actionForReques:(NSDictionary *)requestDic animate:(BOOL)animate andSuceesAction:(WGCBaseObjectActionBlock)successBlock
{
    if ([AppDelegate defaultService].hostReach.currentReachabilityStatus == NotReachable) {
        return;
    }
    
    DTCBaseRequest *object = objc_getAssociatedObject(self, (__bridge const void *)(WGCBaseRequest_instance));
    object = [[super allocWithZone:NULL] init];
    objc_setAssociatedObject(self, (__bridge const void *)(WGCBaseRequest_instance), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    object.requestDic = requestDic;
    
//    [[AlertViewManager shareManager]initWithMessage:[NSString stringWithFormat:@"URL:%@\ndata:%@",[object requestUrl],requestDic] cancleButtonTitle:@"确定" clickAtIndex:^(NSInteger buttonIndex, UIAlertController *alertController) {
//        
//    }];
//    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"URL:%@\ndata:%@",[object requestUrl],requestDic] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
//    [alertView show];
    
//    [object startWithCompletionAnimation:animate andBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
//         [object analyseResponseObject];
//        if (successBlock) {
//            successBlock(object);
//        }
//    } backfailure:^(__kindof YTKBaseRequest * _Nonnull request) {
//        if ([request.responseObject isKindOfClass:[NSDictionary class]]) {
//            if ([request.responseObject[@"errcode"]intValue] == 400011) {
//
//                if (successBlock) {
//                    successBlock(object);
//                }
//            }
//        }else{
//            NSLog(@"%@",request.responseObject);
//        }
//    } requestFailure:^(__kindof YTKBaseRequest * _Nonnull request) {
//        NSLog(@"/n%@",request);
//        NSLog(@"/n%@",request.error);
//    }];
}

+ (id)actionObjectForReques:(NSDictionary *)requestDic animate:(BOOL)animate andSuceesAction:(WGCBaseObjectActionBlock)successBlock
{
    if ([AppDelegate defaultService].hostReach.currentReachabilityStatus == NotReachable) {
        return nil;
    }
    DTCBaseRequest *object = objc_getAssociatedObject(self, (__bridge const void *)(WGCBaseRequest_instance));
    object = [[super allocWithZone:NULL] init];
    objc_setAssociatedObject(self, (__bridge const void *)(WGCBaseRequest_instance), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
//    object.requestDic = requestDic;
//    [object startWithCompletionAnimation:animate andBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
//        [object analyseResponseObject];
//        if (successBlock) {
//            successBlock(object);
//        }
//    } backfailure:^(__kindof YTKBaseRequest * _Nonnull request) {
//        
//    } requestFailure:^(__kindof YTKBaseRequest * _Nonnull request) {
//        
//    }];
    return object;
}

-(id)initWithRequestDictionary:(NSDictionary *)dic
{
    if (self = [super init]) {
        _requestDic = dic;
    }
    return self;
}

- (YTKRequestMethod)requestMethod
{
    return YTKRequestMethodPOST;
}

- (id)requestArgument
{
    NSMutableDictionary *dic = [NSMutableDictionary getPubdic];
    [dic setValuesForKeysWithDictionary:_requestDic];
    NSString *sign=[NSString getSign:dic];
    [dic setObject:sign forKey:@"sign"];
    return dic;
}

-(void)analyseResponseObject
{

}

-(void)dealloc
{
    NSLog(@"\n\n App -> request:%@ dealloc",[self class]);
}
@end
